package com.das.mechanic_base.adapter.main;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.RecyclerView;

import com.das.mechanic_base.R;
import com.das.mechanic_base.app.X3Constent;
import com.das.mechanic_base.bean.main.MainSelectServiceBean;
import com.das.mechanic_base.utils.SpHelper;
import com.das.mechanic_base.utils.X3StringUtils;

import java.util.ArrayList;
import java.util.List;

public class X3MainSelectServiceAdapter extends RecyclerView.Adapter<X3MainSelectServiceAdapter.MainSelectServiceHolder> {

    private Context mContext;

    private List<MainSelectServiceBean> mList;

    private List<Boolean> sList;

    private String moneyCode;

    public X3MainSelectServiceAdapter(Context mContext) {
        this.mContext = mContext;
        this.mList = new ArrayList<>();
        this.sList = new ArrayList<>();
    }

    public void changeServiceItem(List<MainSelectServiceBean> mList) {
        this.mList = mList;
        this.sList.clear();
        for (int i = 0; i < mList.size(); i++) {
            this.sList.add(false);
        }
        if (iOnSelectService != null) {
            iOnSelectService.iOnSelectServiceItem(null);
        }
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public MainSelectServiceHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new MainSelectServiceHolder(LayoutInflater.from(mContext).inflate(R.layout.x3_main_select_service_item,parent,false));
    }

    @Override
    public void onBindViewHolder(@NonNull MainSelectServiceHolder holder, int position) {
        moneyCode = (String) SpHelper.getData(X3Constent.MONEYCODE, "£");
        if (sList.get(position)) {
            holder.tv_charge.setTextColor(Color.parseColor("#0077ff"));
            holder.tv_name.setTextColor(Color.parseColor("#0077ff"));
            holder.ll_item.setBackground(ContextCompat.getDrawable(mContext,R.drawable.x3_main_select_item_shape));
        }else {
            holder.tv_charge.setTextColor(Color.parseColor("#333333"));
            holder.tv_name.setTextColor(Color.parseColor("#333333"));
            holder.ll_item.setBackground(ContextCompat.getDrawable(mContext,R.drawable.x3_alone_add_new_service_item));
        }
        holder.tv_name.setText(mList.get(position).name);
        holder.tv_charge.setText(moneyCode + X3StringUtils.formatDouble(mList.get(position).charge));

        holder.ll_item.setOnClickListener(v -> {
            for (int i = 0; i < sList.size(); i++) {
                sList.remove(i);
                sList.add(i,i == position);
            }
            if (iOnSelectService != null) {
                iOnSelectService.iOnSelectServiceItem(mList.get(position));
            }
            notifyDataSetChanged();
        });
    }

    IOnSelectService iOnSelectService;

    public void setiOnSelectService(IOnSelectService iOnSelectService) {
        this.iOnSelectService = iOnSelectService;
    }

    public interface IOnSelectService {
        void iOnSelectServiceItem(MainSelectServiceBean serviceBean);
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    class MainSelectServiceHolder extends RecyclerView.ViewHolder {
        LinearLayout ll_item;
        TextView tv_name;
        TextView tv_charge;
        public MainSelectServiceHolder(@NonNull View itemView) {
            super(itemView);
            ll_item = itemView.findViewById(R.id.ll_item);
            tv_name = itemView.findViewById(R.id.tv_name);
            tv_charge = itemView.findViewById(R.id.tv_charge);
        }
    }
}
